#pragma once

#include <string>

class Solution {
public:
    bool isValid(const std::string& s)
    {
        std::string stack;
        stack.reserve(s.size());
        for (auto ch : s) {
            switch (ch) {
                case '(':
                case '{':
                case '[': stack.push_back(ch);
                          break;

                case ')': if (stack.empty() || stack.back() != '(')
                              return false;
                          stack.pop_back();
                          break;
                case '}': if (stack.empty() || stack.back() != '{')
                              return false;
                          stack.pop_back();
                          break;
                case ']': if (stack.empty() || stack.back() != '[')
                              return false;
                          stack.pop_back();
                          break;
            }
        }
        return stack.empty();
    }
};
